package com.coollang.smartbra.utils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/11/4 0004.
 */

public class ListUtils {

    public static int getIntMax(List<Integer> arr) {
        int result = arr.get(0);
        for (int x = 1; x < arr.size(); x++) {
            if (arr.get(x) > result)
                result = arr.get(x);
        }
        return result;
    }

    public static Float getFloatMax(List<Float> arr) {
        Float result = arr.get(0);
        for (int x = 1; x < arr.size(); x++) {
            if (arr.get(x) > result)
                result = arr.get(x);
        }
        return result;
    }

    public static int getIntMin(List<Integer> arr) {
        int result = arr.get(0);
        for (int x = 1; x < arr.size(); x++) {
            if (arr.get(x) < result)
                result = arr.get(x);
        }
        return result;
    }

    public static Long getLongMax(List<Long> arr) {
        Long result = arr.get(0);
        for (int x = 1; x < arr.size(); x++) {
            if (arr.get(x) > result)
                result = arr.get(x);
        }
        return result;
    }

    public static Long getLongMin(List<Long> arr) {
        Long result = arr.get(0);
        for (int x = 1; x < arr.size(); x++) {
            if (arr.get(x) < result)
                result = arr.get(x);
        }
        return result;
    }

    public static Float getFloatMin(List<Float> arr) {
        Float result = arr.get(0);
        for (int x = 1; x < arr.size(); x++) {
            if (arr.get(x) < result)
                result = arr.get(x);
        }
        return result;
    }

    public static int getAvge(List<String> list) {
        int sum = 0;
        int total = 0;
        for (int i = 0; i < list.size(); i++) {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(list.get(i));
            if (isNum.matches()) {//判断是否为数字
                sum += Integer.parseInt(list.get(i));
                total += 1;
            }
        }
        if (total != 0) {
            int avg = sum / total;
            return avg;
        } else {
            return 0;
        }
    }

    public static int getSum(List<String> list) {
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(list.get(i));
            if (isNum.matches()) {//判断是否为数字
                sum += Integer.parseInt(list.get(i));
            }
        }
        return sum;
    }

    public static float getFloatSum(List<String> list) {
        float sum = (float) 0;
        for (int i = 0; i < list.size(); i++) {
            sum += Float.parseFloat(list.get(i));
        }
        return sum;
    }
}
